<?php


function cache_add_cal_url($url, $cal_id)
{
    if(is_valid_schedule($cal_id))
    {
        $cp = curl_init($url);
        $local = "phpicalendar/calendars/".$cal_id.".ics";
    	$fp = fopen($local, "w");
	    curl_setopt($cp, CURLOPT_FILE, $fp);
        curl_setopt($cp, CURLOPT_HEADER, 0);
        curl_exec($cp);
        curl_close($cp);
        fclose($fp);
        return true;
    }
    else
    {
        return false;
    }
}

function cache_add_cal($cal_id, $cal_type)
{
    if($cal_type == "scheduleman")
    {
        return cache_add_cal_url('http://scheduleman.org/schedules/'.$cal_id.'.ics', $cal_id);
    }
    else if($cal_type == "tcpulse")
    {
        return cache_add_cal_url('http://sched.tcpulse.com/schedules/'.$cal_id.'.ics', $cal_id);
    }
    else if($cal_type == "url")
    {
        return cache_add_cal_url($cal_id, $cal_id);
    }
    else
    {
        return false;
    }
}

function cache_get_cal($cal_id, $cal_type)
{
    $file = "phpicalendar/calendars/".$cal_id;
    $source = fopen($file, 'r');
    $theData = fread($source, filesize($file));
    fclose($fh);
    return $theData;
}

?>